package yxy.game.pm2.module.chibi;

import org.joda.time.DateTime;
import org.joda.time.LocalDate;
import yxy.apple.util.redisson.RedisException;
import yxy.cherry.data.DataCenter;
import yxy.cherry.data.bean.chibishow;
import yxy.cherry.data.script.fomula;
import yxy.game.pm2.bean.db.dao.MySQLCacheDAO;
import yxy.game.pm2.bean.module.PlayerModule;

import java.util.ArrayList;
import java.util.Collection;

public class ChibiModule extends PlayerModule {
	// bean===========================================================================

	private int times;// 今天的挑战次数
	private int buytimes;// 今天的购买次数
	private ArrayList<ChibiStage> stages = new ArrayList<>();

	private DateTime updateTime;

	public int getTimes() {
		return times;
	}

	public void setTimes(int times) {
		this.times = times;
	}

	public int getBuytimes() {
		return buytimes;
	}

	public void setBuytimes(int buytimes) {
		this.buytimes = buytimes;
	}

	public ArrayList<ChibiStage> getStages() {
		return stages;
	}

	public void setStages(ArrayList<ChibiStage> stages) {
		this.stages = stages;
	}

	public DateTime getUpdateTime() {
		return updateTime;
	}

	public void setUpdateTime(DateTime updateTime) {
		this.updateTime = updateTime;
	}

	// func===========================================================================

	private void update() {
		try {
			if (tryLock()) {
				DateTime now = DateTime.now();
				if (getUpdateTime() == null) {
					setUpdateTime(now);
					set();
				}
				if (!new LocalDate(now).equals(new LocalDate(getUpdateTime()))) {
					setTimes(0);
					setBuytimes(0);
					// 一周结束
					if (now.getWeekOfWeekyear() != getUpdateTime().getWeekOfWeekyear() || now.getWeekyear() != getUpdateTime().getWeekyear()) {
						for (ChibiStage stage : getStages()) {
							stage.setFinish(0);
						}
					}
					setUpdateTime(now);
					set();
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			unlock();
		}
	}

	public ChibiStage stage(int type) {
		for (ChibiStage stage : getStages()) {
			if (stage.getType() == type) {
				return stage;
			}
		}
		return null;
	}

	public int remainTimes() {
		int maxtimes = fomula.火烧赤壁每日挑战次数.intValue();
		int remain = maxtimes - getTimes();
		return remain;
	}

	public int remainBuyTimes() {
		int maxtimes = fomula.火烧赤壁购买次数.intValue();
		int remain = maxtimes - getBuytimes();
		return remain;
	}

	// redis==========================================================================
	private static String key(String playerCode) {
		return String.format("PLAYER:%s:MODULE:CHIBI", playerCode);
	}

	public void set() {
		try {
			setv(key(getPlayerCode()), this);
		} catch (RedisException e) {
			e.printStackTrace();
		}
	}

	static public ChibiModule get(String playerCode) {
		try {
			ChibiModule module = getv(key(playerCode));
			if (module == null) {
				module = MySQLCacheDAO.getV(key(playerCode), ChibiModule.class);
				if(module != null){module.set();}//把数据同步到redis
			}

			if (module == null) {
				module = create(playerCode);
			}
			module.update();
			return module;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	static private ChibiModule create(String playerCode) {
		Collection<chibishow> datas = DataCenter.getAllData(chibishow.class).values();
		ArrayList<ChibiStage> stages = new ArrayList<>();
		for (chibishow data : datas) {
			ChibiStage stage = new ChibiStage();
			stage.setType(data.getType());
			stages.add(stage);
		}

		ChibiModule module = new ChibiModule();
		module.setPlayerCode(playerCode);
		module.setStages(stages);
		module.setTimes(0);
		module.setBuytimes(0);
		module.setUpdateTime(DateTime.now());
		module.set();
		return module;
	}

	//存储到数据库的方法
	public static void saveToDB(String playerCode) {
		try {
			String key = key(playerCode);
			if (touchBucket(key).isExists()) {
				ChibiModule module = getv(key(playerCode));
				MySQLCacheDAO.setV(key, module);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	//从redis移走数据的操作
	public static void removeAllFromRedis(String playerCode) {
		try {
			touchBucket(key(playerCode)).delete();
		} catch (RedisException e) {
			e.printStackTrace();
		}
	}

	// lock==========================================================================

	@Override
	public String lockKey() {
		return String.format("lock:%s", key(getPlayerCode()));
	}

}
